<?php

/**
 * This is the model class for table "sq_message".
 *
 * The followings are the available columns in table 'sq_message':
 * @property integer $Id
 * @property string $Titile
 * @property string $Content
 * @property integer $Created
 * @property integer $Msgtype
 * @property integer $MsgSender
 * @property string $MsgReciever
 * @property integer $MsgSended
 *	@propety  string $UserName
 */
class Message extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Message the static model class
	 */
	public $UserName;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sq_message';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Msgtype, MsgSended,MsgSender', 'numerical', 'integerOnly'=>true),
			array('Titile', 'length', 'max'=>100),
			array('Titile,Content','required'),
			array('Content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, Titile, Content, Created, Msgtype, MsgSender, MsgReciever', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                  'SenderUser'=>array(self::BELONGS_TO,'Userinfo','MsgSender'),
                  'RecieveUser'=>array(self::BELONGS_TO,'Userinfo','MsgReciever'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => '编号',
			'Titile' => '标题',
			'Content' => '消息内容',
			'Created' => '创建时间',
			'Msgtype' => '消息类型',
			'MsgSender' => '发送人',
			'MsgReciever' => '接收人',
			'MsgSended'=>'发送状态',
			'UserName'=>'发送人',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('Titile',$this->Titile,true);
		$criteria->compare('Content',$this->Content,true);
		$criteria->compare('Created',$this->Created);
		$criteria->compare('Msgtype',$this->Msgtype);
		$criteria->compare('MsgSender',$this->MsgSender);
		$criteria->compare('MsgReciever',$this->MsgReciever);
		$criteria->compare('MsgSended',$this->MsgSended);
		$criteria->select="t.*,{{userinfo}}.UserName as UserName ";
		$criteria->join=" LEFT JOIN {{userinfo}} ON t.MsgSender = {{userinfo}}.Id ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function afterFind(){
		if($this->UserName=="")
		{
			$this->UserName="系统用户";
		}
	}

}